
package com.hd.sup.core.rest.client;

import com.hd.common.utils.GenUtil;
import com.hd.common.utils.RestUtils;
import com.hd.sup.config.properties.TransProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title TransRestClient.java
 * @Description 河南运管远程调用工具
 * @Package com.hd.sup.core.rest.client
 * @date 2021/1/13 5:52 下午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
@Slf4j
@Component
public class TransRestClient {

    @Resource
    private TransProperties transProperties;

    /**
     * @Description http重试
     * @Author chentl
     * @Create: 2021/1/13 5:53 下午
     */
    public static final int CONNEC_TIMEOUT = 5000;

    /**
     * @Description 调用超时时间
     * @Author chentl
     * @Create: 2021/1/13 5:53 下午
     */
    public static final int READ_TIMEOUT = 10000;

    /**
     * @Description 重试次数
     * @Author chentl
     * @Create: 2021/1/13 5:53 下午
     */
    public static final int RETRY_COUNT = 3;

    /**
     * @Description 请求头
     * @Author chentl
     * @Create: 2021/1/13 5:53 下午
     */
    public static Map<String, Object> headerMap = new HashMap<>();

    /**
     * @Description CONTENT_TYPE
     * @Author chentl
     * @Create: 2021/1/13 5:53 下午
     */
    public static final String CONTENT_TYPE = "Content-Type";
    public static final String APPLICATION_JSON = "application/json";

    static {
        headerMap.put(CONTENT_TYPE, APPLICATION_JSON);
    }


    /**
     * @return java.lang.String
     * @author chentl
     * @description 从联网联控中心 获取车辆信息
     * @date 2021/1/13 6:33 下午
     * @params [req]
     */
    public String getVehicleIdInfo(String vid) {
        log.info("[rest] 调用 车辆基础信息API , 参数: {}", vid);
        Long startTime = System.currentTimeMillis();
        String url = transProperties.getGetVehicleIdInfo() + "?uc=" + transProperties.getUserCode() + "&et=" + GenUtil.getECode(transProperties.getUserCode()) + "&vid=" + vid;
        String result = "";
        try {
            result = RestUtils.getHttp(url);
            Long end = System.currentTimeMillis();
            log.info("[rest] 调用成功，返回值：{}，花费时间: {} ms", result, end - startTime);
        } catch (Exception e) {
            log.error("[rest] 调用 车辆基础信息API 时发生异常，{}", result == null ? "接口返回结果为空" : e.getMessage());
        }
        return result;
    }

    /**
     * @return java.lang.String
     * @author chentl
     * @description 获取企业基础信息
     * @date 2021/1/13 6:35 下午
     * @params [req]
     */
    public String getPermission(String on, String mc) {
        log.info("[rest] 调用 企业基础信息API , 参数:on:{} ,mc:{}", on, mc);
        Long startTime = System.currentTimeMillis();
        String url = transProperties.getGetPermission() + "?uc=" + transProperties.getUserCode() + "&et=" + GenUtil.getECode(transProperties.getUserCode()) + "&on=" + on + "&mc=" + mc;
        String result = "";
        try {
            result = RestUtils.getHttp(url);
            Long end = System.currentTimeMillis();
            log.info("[rest] 调用成功，返回值：{}，花费时间: {} ms", result, end - startTime);
        } catch (Exception e) {
            log.error("[rest] 调用 企业基础信息API 时发生异常，{}", result == null ? "接口返回结果为空" : e.getMessage());
        }
        return result;
    }


}
